const {Craw} = require('../app');
const url = 'https://www.meizu.com/index.html';
let Crawler = new Craw({url});
const {insertData} = require('../server/mongoose/common')
// 轮播图
async function swipe() {
    let ImgUrl = await Crawler.PageDown({
        async fn(page) {
            return await page.$$eval('#mz-index-banner > div.swiper-container-banner > div.swiper-wrapper > div.slider-1', (dom) => {
                dom.shift();
                dom.pop()
                let dataUrl = dom.map((item) => {
                    return item.style['backgroundImage'].split('url').pop().replace(/(^\(\")|(\"\)\s+2x\)$)/gi, '')
                })
                return dataUrl
            })
        }
    })
    return Promise.resolve(ImgUrl);
}

async function section_box_adv() {
    let section_box_adv_url = await Crawler.PageDown({
        async fn(page) {
            return page.$$eval('#index-adv-center > div > div > ul li img', (dom) => {
                return dom.map((item) => {
                    return item.src
                })
            })
        }
    });
    return section_box_adv_url
}

// 热卖商品
async function index_center_wrapper() {
    let index_center_wrapper_data = await Crawler.PageDown({
        fnc: function () {
            let img = document.querySelectorAll('#index-hotsale-center > div > div.section-box-row > div > ul li a img');
            let span = document.querySelectorAll('#index-hotsale-center > div > div.section-box-row > div > ul li a span.box-info');
            let imgUrl = [].map.call(img, (item) => {
                return {src: item.src}
            });

            [].forEach.call(span, (item, index) => {
                imgUrl[index].goodsName = item.querySelectorAll('span')[0].innerText;
                imgUrl[index].goodsDesc = item.querySelectorAll('span')[1].innerText;
                imgUrl[index].goodsPrice = item.querySelectorAll('span')[2].innerText;
            })
            console.log(imgUrl)
            return imgUrl
        }
    })
    return index_center_wrapper_data
}

// 手机推荐
async function section_phone_box() {
    let section_phone_box_data = await Crawler.PageDown({
        fnc: function () {
            let imgSrc = document.querySelectorAll('#index-phone-center > div > div.section-box-recomend.clearfix img');

            let iphome = [].map.call(imgSrc,item => {
                let url = item.getAttribute('srcset').replace(/\s+\d+\w+$/gi,'')
                return {recommendSrc:url}
            })

            let  commodity = document.querySelectorAll('#index-phone-center > div > div.section-box-row > div > ul li a');

            let commodityUrl = [].map.call(commodity,item => {

                let imgurl = item.querySelector('img').src;
                let span = item.querySelectorAll('.box-info span');

                return {
                    src:imgurl,
                    goodsName: span[0].innerText,
                    goodsDesc: span[1].innerText,
                    goodsPrice: span[2].innerText
                }
            })
            return [iphome,commodityUrl]
        }
    })
    return section_phone_box_data
}

// 智能配件
async function index_accessory_center() {
    let index_accessory_center_data = await Crawler.PageDown({
        fnc: function () {
            let sectionBoxRecomend = document.querySelectorAll('#index-accessory-center > div > div.section-box-recomend.clearfix img');
            //
            let RecomendImgUrl = [].map.call(sectionBoxRecomend,(item) => {
                return {ImgUrl:item.src}
            });


            let wrapper = document.querySelectorAll('#index-accessory-center > div > div.section-box-row > div > ul li a');
            let ImgUrl = [].map.call(wrapper, item => {
                let img = item.querySelector('img');
                let span = item.querySelectorAll('.box-info span')
                return {
                    ImgUrl:img.src,
                    goodsName: span[0].innerText,
                    goodsDesc: span[1].innerText,
                    goodsPrice: span[2].innerText
                }
            })
            return [RecomendImgUrl,ImgUrl]
        }
    })
    return Promise.resolve(index_accessory_center_data);
}

// 导航
async function site_nav_bd() {
    let site_nav_bd_data = await Crawler.PageDown({
        fnc:function () {
            // 魅族手机
            let hd603 = document.querySelectorAll('#meizu-header-sub-hd603 > div > ul li a');
            let hd603_data = [].map.call(hd603,item => {
                return {
                    imgUrl: item.querySelector('img').getAttribute('data-src'),
                    name: item.innerHTML.match(/(>).+(?=<span>)/g).join('').slice(1),
                    price: item.querySelector('em').innerHTML
                }
            })

            // 没蓝手机
            let hd604= document.querySelectorAll('#meizu-header-sub-hd604 > div > ul li a');
            let hd604_data = [].map.call(hd604,item => {
                return {
                    imgUrl: item.querySelector('img').getAttribute('data-src'),
                    name: item.innerHTML.match(/(>).+(?=<span>)/g).join('').slice(1),
                    price: item.querySelector('em').innerHTML
                }
            })

            // 魅族声学
            let hd605= document.querySelectorAll('#meizu-header-sub-hd605 > div > ul li a');
            let hd605_data = [].map.call(hd605,item => {
                return {
                    imgUrl: item.querySelector('img').getAttribute('data-src'),
                    name: item.innerHTML.match(/(>).+(?=<span>)/g).join('').slice(1),
                    price: item.querySelector('em').innerHTML
                }
            })

            // 只能配件
            let hd606= document.querySelectorAll('#meizu-header-sub-hd606 > div > ul li a');
            let hd606_data = [].map.call(hd606,item => {
                return {
                    imgUrl: item.querySelector('img').getAttribute('data-src'),
                    name: item.innerHTML.match(/(>).+(?=<span>)/g).join('').slice(1),
                    price: item.querySelector('em').innerHTML
                }
            })

            // 生活周边
            let hd607= document.querySelectorAll('#meizu-header-sub-hd607 > div > ul li a');
            let hd607_data = [].map.call(hd607,item => {
                return {
                    imgUrl: item.querySelector('img').getAttribute('data-src'),
                    name: item.innerHTML.match(/(>).+(?=<span>)/g).join('').slice(1),
                    price: item.querySelector('em').innerHTML
                }
            })
            return [
                {hd603:hd603_data},
                {hd604:hd604_data},
                {hd605:hd605_data},
                {hd606:hd606_data},
                {hd607:hd607_data},
            ]
        }
    })
    return site_nav_bd_data
};
// 底部
async function meizu_footer_link() {
    let meizu_footer_link_data = await Crawler.PageDown({
        fnc: function () {
            let footerService= document.querySelectorAll('#meizu-footer > div > div > div.meizu-row.clearfix > ul li a');
            console.log(footerService[0].querySelector('span').innerText)
            let footerServiceData = [].map.call(footerService, item => {
                return {
                    imgUrl: item.querySelector('img').src,
                    severText: item.querySelector('span').innerText
                }
            })
            return footerServiceData
        }
    })
    return meizu_footer_link_data
};


// swipe();

(async () => {
    let swipes = await swipe();
    let section_box_advs = await section_box_adv();
    let index_center_wrappers = await index_center_wrapper();
    let section_phone_boxs = await section_phone_box();
    let index_accessory_centers = await index_accessory_center();
    let site_nav_bds = await site_nav_bd();
    let  meizu_footer_links = await meizu_footer_link();
    insertData('homedatas').then(model => {
        model.create({
            swipe: swipes,
            section_box_adv: section_box_advs,
            index_center_wrapper: index_center_wrappers,
            section_phone_box: section_phone_boxs,
            index_accessory_center: index_accessory_centers,
            site_nav_bd: site_nav_bds,
            meizu_footer_link: meizu_footer_links
        })
    })
})();
